home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------------------------
- // Event Handling Code
- //
- // by Philip McBride
- // parts freely taken from Apple DTS sample code
- //
- //--------------------------------------------------------------------------------------------
-
-
- #include "GameControls.h"
- #include "extern.h"
- #include "event.h"
- #include "AEVT.h"
- #include "inits.h"
- #include "document.h"
- #include "file.h"
- #include "draw.h"
-
- //--------------------------------------------------------------------------------------------
- // Event Loop
- //
- void EventLoop(void)
- {
- EventRecord theEvent;
- RgnHandle theMouseRgn;
-
- theMouseRgn = NewRgn();
-
- while (! gQuit) {
- if (WaitNextEvent(everyEvent, &theEvent, 0L, theMouseRgn))
- DoEvent(&theEvent);
- }
-
- DisposeRgn(theMouseRgn);
- }
-
- //--------------------------------------------------------------------------------------------
- // Handle any Event
- //
- void DoEvent(EventRecord *theEvent)
- {
- short thePart;
- WindowPtr theWindow ;
- Rect screenRect;
- Point aPoint = {100, 100};
-
- switch(theEvent->what) {
- case nullEvent:
- break;
-
- case mouseDown:
- thePart = FindWindow( theEvent->where, &theWindow );
-
- switch( thePart ) {
- case inMenuBar:
- MyAdjustMenus() ;
- HandleMenuCommand(MenuSelect(theEvent->where));
- break;
-
- case inDrag:
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( theWindow, theEvent->where, &screenRect );
- break ;
-
- case inContent:
- if (theWindow != FrontWindow())
- SelectWindow( theWindow );
- else
- HandleMouseDown(theEvent);
- break ;
-
- case inGoAway:
- if (TrackGoAway( theWindow, theEvent->where )) {
- MyCloseDocument(MyGetDocumentFromWindow(theWindow));
- }
- break ;
-
- default:
- break ;
- }
- break ;
-
- case updateEvt:
- theWindow = FrontWindow();
- BeginUpdate( theWindow );
- MyUpdateScreen(MyGetDocumentFromWindow(theWindow));
- EndUpdate( theWindow );
- break ;
-
- case mouseUp:
- break;
-
- case keyDown:
- case autoKey:
- HandleKeyPress(theEvent);
- break;
-
- case diskEvt:
- if ( HiWrd(theEvent->message) != noErr )
- (void) DIBadMount(aPoint, theEvent->message);
- break;
-
- case activateEvt:
- break;
- case osEvt:
- break;
-
- case kHighLevelEvent:
- DoHighLevelEvent(theEvent);
- break;
- }
- }
-
- //--------------------------------------------------------------------------------------------
- // Handle Key Down
- //
- void HandleKeyPress(EventRecord *theEvent)
- {
- char key;
- WindowPtr theWindow;
-
- key = theEvent->message & charCodeMask;
- theWindow = FrontWindow();
-
- // If command key down, then it's a menu command,
- // otherwise we're moving the camera with the keyboard.
- if ( theEvent->modifiers & cmdKey ) {
- HandleMenuCommand(MenuKey(key));
- } else {
- MyDoKeyMove(theWindow, theEvent, key);
- }
- }
-
- //--------------------------------------------------------------------------------------------
- // Handle Mouse Down
- //
- void HandleMouseDown(EventRecord *theEvent)
- {
- WindowPtr theWindow;
-
- theWindow = FrontWindow();
- MyDoMouseMove(theWindow, theEvent);
- }
-
- //--------------------------------------------------------------------------------------------
- // Handle the About Application Event
- //
-
- void HandleAboutApp( void )
- {
- ModalFilterUPP theProc ;
- DialogPtr theDialog ;
- short itemHit ;
-
- theDialog = GetNewDialog ( kMyAboutDialogID, nil, (WindowPtr)-1 );
-
- // these two lil' snappers are system 7 only
- // so if you use them, check before!!
- // in this app we will only run on Power
- // Macintosh, so we don't check
-
- GetStdFilterProc( &theProc ) ;
- SetDialogDefaultItem(theDialog, ok) ;
-
- // put the dialog up and loop 'til
- // the user hits the OK button
-
- do {
- ModalDialog ( theProc, &itemHit );
- } while( itemHit != ok ) ;
-
- DisposDialog ( theDialog );
- }
-
- //--------------------------------------------------------------------------------------------
- // Handle an Open Document
- //
- OSErr HandleOpenDoc(FSSpec *theFile)
- {
- OSErr err ;
- DocumentPtr theDocument ;
- WindowPtr theWindow ;
-
- // Create a new document and associated window.
- theDocument = MyNewDocument();
- theWindow = (WindowPtr)theDocument->theWindow ;
-
- // Open the file.
- err = MyOpenFile( theFile, theDocument ) ;
- if (err == noErr) {
- MyAdjustMenus() ;
- MyUpdateScreen(theDocument) ;
- } else {
- MyCloseDocument(theDocument);
- }
-
- return err ;
- }
-
-
- //--------------------------------------------------------------------------------------------
- // Handle a Menu Command
- //
- void HandleMenuCommand(long menuResult)
- {
- short menuID;
- short menuItem;
- Str255 daName;
-
- short numTypes = 2 ;
- SFTypeList myTypes = { '3DMF', 'TEXT' } ;
-
- DocumentPtr theDocument ;
- TQ3ViewObject theView ;
- WindowPtr theWindow ;
-
- StandardFileReply theSFReply ;
-
- menuID = HiWrd(menuResult);
- menuItem = LoWrd(menuResult);
-
- switch ( menuID ) {
- //
- //--------------------------------------------------------------------------
- //
- case mApple:
- switch ( menuItem ) {
-
- case iAbout:
- HandleAboutApp() ;
- break ;
-
- default:
- GetItem(GetMHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
- //
- //--------------------------------------------------------------------------
- //
- case mFile:
- switch ( menuItem ) {
- case iOpen:
- // Get the file name to open
- StandardGetFile( nil, numTypes, myTypes, &theSFReply ) ;
-
- // If the user didn't cancel, open the file.
- if(theSFReply.sfGood)
- HandleOpenDoc(&theSFReply.sfFile) ;
-
- break ;
-
- case iClose:
- MyCloseDocument( MyGetDocumentFromWindow ( FrontWindow() ));
- break ;
-
- case iQuit:
- MySendQuitApp();
- break;
- }
- break;
-
-
- //
- //--------------------------------------------------------------------------
- //
- case mView:
-
- theWindow = FrontWindow() ;
-
- if( theWindow == nil )
- break ;
-
- theDocument = MyGetDocumentFromWindow( theWindow ) ;
- theView = theDocument->theView ;
-
- switch(menuItem)
- {
- case iWireframe:
- {
- TQ3RendererObject myRenderer;
-
- // set the renderer to wireframe
- myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
- Q3View_SetRenderer(theView, myRenderer);
-
- Q3Object_Dispose( myRenderer ) ;
- }
- break;
-
- case iInteractive:
- {
- TQ3RendererObject myRenderer;
-
- // set the renderer to use the interactive renderer
- myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
- Q3View_SetRenderer(theView, myRenderer);
-
- Q3Object_Dispose( myRenderer ) ;
- }
- break;
-
- default:
- break;
- }
-
- // Update screen.
- MyUpdateScreen(theDocument);
- break;
-
- }
- HiliteMenu(0); // Unhighlight whatever MenuSelect or MenuKey hilited
- }
-